<?php

require_once 'KgridList.php';

//$this->_KgridListing =  new KgridList();
$KgridListing =  new KgridList();




$strAction = $_REQUEST["action"];           //get action from another web page

$strPK_ID = $_REQUEST["num"]; 

//$intRowIDGet = ""; 
//$intPageIDGet = "";  
//$intSkipGet = "";  
$intRowIDGet = $_REQUEST["rowid"]; 
$intPageIDGet = $_REQUEST["pageid"];  
$intSkipGet = $_REQUEST["skip"];  


//get Sorted by field and PageSize, that are passed as well
$strSortField = $_REQUEST["sortby"];  
$strPageSizeGet = $_REQUEST["pagesize"];  

  if ($strPageSizeGet == ""
            || strlen($strPageSizeGet) == 0)
 {
                  $intPageSizeGet = 1;     //so as not to divide by 0
 }
 else
 {
                  $intPageSizeGet = $strPageSizeGet;
 }





if (isset($_REQUEST["startdatetosave"]))
{
	$strstartdatetosave = $_REQUEST["startdatetosave"];
}
	else
{
    $strstartdatetosave = "";
}


if (isset($_REQUEST["datenametosave"]))
{
	$strdatenametosave = $_REQUEST["datenametosave"];
}
	else
{
    $strdatenametosave = "start_date";
}



// Check to see if field changed is one sorted on
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
 
//now workout if run SkipCount (ie sort field changed)
$len = strlen($strSortField);
$intf = strpos($strSortField, " ");
if ($intf > 1)
{
 $strSortBy = substr($strSortField,0,$intf);
}
//echo $strSortBy;
$strDoSkip = "False";


if ($strdatenametosave == "Start_Date")
{
  if ($strSortBy == "Start_Date")  
  {
    $strDoSkip = "True";
  }
}
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&



//END Fields to be Updated
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&


//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
//Next Sort and Search fields are for if using LIST in conjunction (Do not need for Standalone Update form)
//----------------------------------------------------------------------------------------------------------
                $strarraysearch = $_REQUEST["arraysearch"];  
                //$strarraysearch = "";  
                $strmachsearch = "";
             
//END Optional Fields               
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&



              UpdateDBase($strSortField, $strPageSizeGet, $strPK_ID, $strdatenametosave, $strstartdatetosave, $strDoSkip);


               //$strReinitialise = "";
               $strReinitialise = " False ";


                if ($blPageValid == "false") 
                {
                     //Response.Write(strErrMsg);
                     echo $strErrMsg;
                     //Response.End();
                }
                else
                {
                  if ($strAction == "byitself")           //DO not refresh LIST (if used)
                  {
                      //Response.Write(" ");      //pass back nothing
                      echo " ";
                      //Response.End();
                  }
                  else
                  { 

                    //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
                    //the next line is used also to check if Sort and Search Criteria fields have been changed (ie strReinitialise parameter)
                    //The next part is for using LIST in conjunction (Do not need for Standalone Update form)


                    if ($runSkipCount == "True")
                    {
                      $intRowIDGet = $intRowIDGetGlobal;
                      $intPageIDGet = $intPageIDGetGlobal;
                      $intSkipGet = $intSkipGetGlobal;
                      $strarraysearch = "";          //set search criteria as blank
                      $strReinitialise = " True ";   //set search criteria as blank
                    }

                     //echo $runSkipCount;
                     //echo $strPageSizeGet;


                     $ActionPage = "GetCurrentPage";


                     $KgridListing->ajaxkgridlist($ActionPage, $strarraysearch, $strmachsearch, $intPageIDGet, $strSortField, $intSkipGet, $intRowIDGet, $strPK_ID, $strReinitialise);


                  }       //strAction = "byitself"

                }         //$blPageValid == "false"




    function GetSkipCount($strSortField, $strPageSizeGet, $strPK_ID)
    {
    //--------------------------------------------------------------------------------------------------------------------
    // PURPOSE SUMMARY:- Re-calculate PageID, RowID and SkipCount (if search or sort fields changed)
    //                                       So as to display in List correctly.
    //
    // PSEUDO CODE:-  Select PK_ID from Array table using unique Sortedby key
    //                              Read all records until PK_ID of record on page matches
    //                               when match found exit out read immediately
    //                               Re-calculate PageID, RowID and SkipCount, using derived RecordCount & PageSize
    //---------------------------------------------------------------------------------------------------------------------

        global $runSkipCount;
        $intwherecounter = 0;
        $intCounter = 0;
        //SqlCommand objCmd;
        $strSQL = "";
         
        $dsn2="SQLSERVER2005";
        $username2="sa";
        $password2="and";
    
        $sqlconnect2=odbc_connect($dsn2,$username2,$password2);

         //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
         //Change as required
         $strSQL = "SELECT PK_ID FROM ARRAYS2 ";                             //customise to your Table
         $strSQL = $strSQL . " ORDER BY " . $strSortField;                    //customise to your Table
         //$strSQL = $strSQL . " ORDER BY PK_ID";                             //customise to your Table
         //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

        //echo $strSQL;

        $rs=odbc_exec($sqlconnect2, $strSQL);


        //if (!$rs)
        // {
        //    echo "error in Connecting to do Count";
        // }

         $intRecordCount = 0;
         $strnotfound = "Yes";

         while (odbc_fetch_row($rs))
         {

            $intRecordCount = $intRecordCount + 1;

           if (odbc_result($rs,"PK_ID") == $strPK_ID)
           {
              $strnotfound = "No";
              break;
           }
 
          
         }

        odbc_close($sqlconnect2);

        global $intRowIDGetGlobal;
        $intRowIDGetGlobal = 0;
        global $intPageIDGetGlobal;
        $intPageIDGetGlobal = 0;
        global $intSkipGetGlobal;
        $intSkipGetGlobal = 0;

        $intresult = $intRecordCount / $strPageSizeGet;
        $intresult = Floor($intresult);
        $intremainder = $intRecordCount % $strPageSizeGet;

        

        if ($intremainder == 0) 
        {
          $intRowIDGetGlobal = ($strPageSizeGet - 1);                                 //must always take 1 off pageid (ie because row 1 is now row 0, for AJAX grid list)
          if ($intRowIDGetGlobal < 0)                                //check tha rowid is not < 0
          {
            $intRowIDGetGlobal = 0;
          }
          $intPageIDGetGlobal = $intresult;
          $intSkipGetGlobal = ($intresult * $strPageSizeGet) - $strPageSizeGet;
        }

        if ($intremainder > 0) 
        {
          $intRowIDGetGlobal = ($intremainder - 1);                                      //must always take 1 off pageid (ie because row 1 is now row 0, for AJAX grid list)
          if ($intRowIDGetGlobal < 0)                              //check tha rowid is not < 0
          {
            $intRowIDGetGlobal = 0;
          }
          $intPageIDGetGlobal = $intresult + 1;
          $intSkipGetGlobal = (($intresult + 1) * $strPageSizeGet) - $strPageSizeGet;
        }


        $runSkipCount = "True";

         //echo $intCounter;

        //return $intCounter;

    }



    function GetSkipCountNothing($strSortField, $strPageSizeGet, $strPK_ID)
    {
    
        global $runSkipCount;
        $runSkipCount = "False";
    
    }



    function UpdateDBase($strSortField, $strPageSizeGet, $strPK_ID, $strdatenametosave, $strstartdatetosave, $strDoSkip)
    {

       global $strErrMsg;
       global $blPageValid; 
       $blPageValid = "true";
       
 
        if ($strstartdatetosave == ""
                    || (strlen($strstartdatetosave) == 0))
        {
			 $strStartDate = "NULL";
             //OK
		}
        else
        {
              //$dt="28/02/2007";
		      $dt = $strstartdatetosave;
              $arr=split("/",$dt); // splitting the array
              $dd=$arr[0]; // first element of the array is month
              $mm=$arr[1]; // second element is date
              $yy=$arr[2]; // third element is year
              If(!checkdate($mm,$dd,$yy))
		      {
               $strErrMsg = "Error - Invalid Start Date!";
               $blPageValid = "false";
			  }
              else
			  {
                 $strStartDate = $strstartdatetosave;
			  }
        }

      


      if ($blPageValid == "true")
      {


          if ($strStartDate == "NULL")
          {
                //nothing
          }
          else
          {
                $dt = $strStartDate;
                $arr=split("/",$dt); // splitting the array
                $strstartday=$arr[0]; // first element of the array is month
                $strstartmonth=$arr[1]; // second element is date
                $strstartyear=$arr[2]; // third element is year
			    //$strstartyear = substr($strStartDate,0,4);
                //$strstartmonth = substr($strStartDate,5,2);
                //$strstartday = substr($strStartDate,8,2);
                $my_date = gmmktime(0,0,0,$strstartmonth,$strstartday,$strstartyear );
                $strStartDate = date("Ymd",$my_date);
                //ws_conv_date = DateTime.Parse(strstartdatetosave);
                //strStartDate = DateConvitfromformUS(ws_conv_date);        //yyyymmdd isodate format  
                $strStartDate = "'" . $strStartDate . "'";
          }




        $dsn2="SQLSERVER2005";
        $username2="sa";
        $password2="and";
        
        $odbc = odbc_connect($dsn2,$username2,$password2);

       
        $update = "UPDATE ARRAYS2 SET " . $strdatenametosave . " = " . $strStartDate . "  ";
        $update = $update . "WHERE PK_ID = " . $strPK_ID . "";

        //echo $update;
		
        odbc_exec($odbc,$update);

        odbc_close($odbc);



        if ($strDoSkip == "True")
        {
          GetSkipCount($strSortField, $strPageSizeGet, $strPK_ID);
        }
        else
        {
          GetSkipCountNothing($strSortField, $strPageSizeGet, $strPK_ID);
        }
        

      }        //$blPageValid = true

   }







?>
